package com.luoxiang.socialandroid.adapter;

import android.annotation.SuppressLint;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.luoxiang.socialandroid.R;
import com.luoxiang.socialandroid.bean.AgeBean;
import com.luoxiang.socialandroid.bean.EmotioRelation;
import com.luoxiang.socialandroid.bean.TraitTargetAge;

import java.util.List;

public class Tezhi3RecyAdapter extends RecyclerView.Adapter<Tezhi3RecyAdapter.ViewHolder>{
    private List<TraitTargetAge> list;
    private int mp = 0;
    public Tezhi3RecyAdapter(List<TraitTargetAge> list) {
        this.list = list;
    }

    public static interface OnAddClickListener {
        // true add; false cancel
        public void onItemClick(int id); //传递boolean类型数据给activity
    }

    OnAddClickListener onItemAddClick;
    public void setOnAddClickListener(OnAddClickListener onItemAddClick) {
        this.onItemAddClick = onItemAddClick;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_tezhi3, parent, false);
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, @SuppressLint("RecyclerView")int position) {
        TraitTargetAge bean = list.get(position);
        holder.tx_tezhi3.setText(bean.getTitle());
        if(position == mp){
            bean.setSelect(true);
            holder.tx_tezhi3.setTextColor(Color.parseColor("#FFFFFF"));
            holder.itemView.setBackgroundResource(R.drawable.shape_age_cbg);
        }else {
            holder.tx_tezhi3.setTextColor(Color.parseColor("#999999"));
            holder.itemView.setBackgroundResource(R.drawable.shape_age_bg);
        }
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //多选
//                bean.setSelect(!bean.isSelect());
//                notifyDataSetChanged();
//                单选放开
                for (TraitTargetAge item : list) {
                    if (item.getId() == bean.getId()) {
                        item.setSelect(!item.isSelect());
                        mp = position;
                    } else {
                        item.setSelect(false);
                        if(onItemAddClick != null) {
                            onItemAddClick.onItemClick(bean.getId());
                        }
                    }
                    notifyDataSetChanged();
                }
            }

        });
        if (bean.isSelect()) {
            holder.tx_tezhi3.setTextColor(Color.parseColor("#FFFFFF"));
            holder.itemView.setBackgroundResource(R.drawable.shape_age_cbg);
        } else {
            holder.tx_tezhi3.setTextColor(Color.parseColor("#999999"));
            holder.itemView.setBackgroundResource(R.drawable.shape_age_bg);
        }
    }

    @Override
    public int getItemCount() {
        return list.size();
    }
    public class ViewHolder extends RecyclerView.ViewHolder {
        TextView tx_tezhi3;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            tx_tezhi3 = itemView.findViewById(R.id.tx_tezhi3);
        }
    }
}
